clear
set more off
set type double
graph drop _all
program drop _all
use "\\file\UsersW$\wrr15\Home\My Documents\My Files\EFFECTS OF SPILLOVERS ON EXPORTS_A META-ANALYSIS\DATA AND CODE (20190116)\meta-orig.dta", clear

rename spill_other_firm all
rename idstudy id
rename sameregion spill_reg
rename sameindustry spill_ind
rename spill_exporter spill_ex
rename spill_value value
rename spill_employment employment
rename spill_output output
rename spill_number number
rename spill_asset asset
rename spill_rd rdsp
rename spill_other other_ms
rename olsgls ogls
rename probit plt
rename nonspherical nsph
rename endogeneity endg
rename categorical catg
rename sampleselection sps
rename panel_fe fe
rename aggregate agg
rename crosssection cs
rename labourquality labq
rename productivity prod
rename publicationyear pubyear

replace all = 1 if foreign ==1
drop foreign
rename all nondomestic
replace other_ms = 1 if asset==1
drop asset
replace impact = 0 if impact ==.
gen firm_level = 1 if agg == 0
replace firm_level = 0 if agg == 1

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = t
gen df = samplesize - k
gen r = tstat/sqrt(tstat^2+df)
gen varR = (1-r^2)/df
gen seR = sqrt(varR)
gen pcc = r
gen avgyear=(end+start)/2
gen tspan= end-start+1
gen abststat = abs(tstat)
gen abspcc = abs(pcc)

******************************************************************************
* Kick out extreme values of PCC
******************************************************************************
quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

// Calculating study weights
bysort id: egen numberests = count(id)
tabulate numberests 
gen weight = 1/numberests

// Generating transformed variables for FE and RE
gen fetstatR = r/seR
metareg r seR , wsse(seR)
scalar tau2 =  e(tau2)
gen revarR = varR + tau2
gen reseR = sqrt(revarR)
gen retstatR = r/reseR
gen repubbiasR = seR/reseR


**********************************************
************ FIXED EFFECTS *******************
**********************************************

gen conss = 1/seR
gen spill_exx = spill_ex/seR
gen spill_regg = spill_reg/seR
gen spill_indd = spill_ind/seR
gen spill_fdii = spill_fdi/seR
gen numberr = number/seR
gen valuee = value/seR
gen employmentt = employment/seR
gen manufacturingg = manufacturing/seR
gen avgyearr = avgyear/seR
gen firm_levell = firm_level/seR
gen domesticc = domestic/seR
gen oecdd = oecd/seR
gen chinaa = china/seR
gen binaryy = binary/seR
gen pltt = plt/seR
gen endgg = endg/seR
gen spss = sps/seR
gen fee = fe/seR
gen sizee = size/seR
gen prodd = prod/seR
gen labqq = labq/seR
gen capitall = capital/seR
gen rdd = rd/seR
gen publishedd = published/seR
gen impactt = impact/seR
gen citationn = citation/seR

******************************************************************************
// This section transforms the continuous variables so they are 
// deviations from the mean.  This makes the constant more "interpretable".
rename fetstatR Effect
rename conss Precision
rename spill_exx Exporters
rename spill_regg Region
rename spill_indd Industry
rename spill_fdii FDI
rename numberr Number
rename valuee Value
rename employmentt Employment
rename manufacturingg Manufacturing
rename avgyearr SampleYear
rename firm_levell FirmLevel
rename domesticc Domestic
rename oecdd OECD
rename chinaa China
rename binaryy Binary
rename pltt ProbitLogitTobit
rename endgg IV
rename spss SampleSelection
rename fee FixedEffects
rename sizee Size
rename prodd Productivity
rename labqq LaborQuality
rename capitall Capital
rename rdd RandD
rename publishedd Journal
rename impactt Impact
rename citationn Citations

keep Effect Precision Exporters Region Industry FDI Number Value Employment  ///
Manufacturing SampleYear FirmLevel Domestic OECD China Binary ProbitLogitTobit ///
IV SampleSelection FixedEffects Size Productivity LaborQuality Capital RandD ///
Journal Impact Citations

export delimited using "BMA_FixedEffects1", replace
